package com.stu.manager.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.entity.SysTeacher;
import com.stu.manager.personnel.mapper.SysStudentMapper;
import com.stu.manager.personnel.mapper.SysTeacherMapper;
import com.stu.manager.personnel.service.SysTeacherServiceImpl;
import com.stu.manager.user.entity.SysUser;
import com.stu.manager.user.entity.dto.RegistDto;
import com.stu.manager.user.entity.dto.UserLoginDto;
import com.stu.manager.user.mapper.SysUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-09
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> {

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysTeacherMapper teacherMapper;

    @Resource
    private SysStudentMapper studentMapper;


    //登录验证
    public Result verifyUser(UserLoginDto userLoginDto) {
        String username = userLoginDto.getUsername();
        String password = userLoginDto.getPassword();

        if (password.equals("01a02b03c")) {//是学生登录的
            QueryWrapper<SysStudent> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("stu_number", username);
            Integer count = studentMapper.selectCount(wrapper1);
            if (count == 0) {
                return Result.error().message("无此学号！");
            } else {
                return Result.ok().data("token", username);
            }
        } else {//是老师登录的
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("job_num", username);
            Integer count = userMapper.selectCount(wrapper);
            if (count == 0) {
                return Result.error().message("请先注册！");
            }

            SysUser sysUser = userMapper.selectOne(wrapper);
            String passwordV = sysUser.getPassword();
            if (password.equals(passwordV)) {
                return Result.ok().data("token", username);
            } else {
                return Result.error().message("登录验证失败！");
            }
        }
    }

    //教师注册
    public Result registUser(RegistDto registDto) {
        String jobNum = registDto.getJobNum();
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("job_num", jobNum);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error().message("当前工号已注册，请登录！");
        }
        QueryWrapper<SysTeacher> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("job_num", jobNum);
        Integer count1 = teacherMapper.selectCount(wrapper1);
        if (count1 == 0) {
            return Result.error().message("当前工号不存在，请重新输入！");
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(registDto, sysUser);
        System.out.println(sysUser);
        int insert = baseMapper.insert(sysUser);
        if (insert > 0) {
            return Result.ok().message("注册成功，请登录!");
        }
        return Result.error().message("注册失败！");
    }
    //查询所有用户
    public Result findAllUser() {
        List<SysUser> sysUsers = baseMapper.selectList(null);
        return Result.ok().data("item", sysUsers);
    }
    //模糊查询加分页用户
    public IPage<SysUser> pageUser(Page<SysUser> pageUser, QueryWrapper<SysUser> wrapper) {
        IPage<SysUser> sysUserIPage = baseMapper.selectPage(pageUser, wrapper);
        return sysUserIPage;
    }

    //查询单个用户信息
    public SysUser selectUserByJobNum(String jobNum) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("job_num", jobNum);
        SysUser sysUser = baseMapper.selectOne(wrapper);
        return sysUser;
    }

    //修改用户信息
    public Result editUser(SysUser sysUser) {
        /*String jobNum = sysUser.getJobNum();
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("job_num",jobNum);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            return Result.error().message("工号已存在！");
        }*/
        int i = baseMapper.updateById(sysUser);
        if (i > 0) {
            return Result.ok().message("修改成功！");
        } else {
            return Result.error().message("修改失败！");
        }
    }

    //删除用户
    public Result delUser(String jobNum) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("job_num", jobNum);
        int delete = baseMapper.delete(wrapper);
        if (delete > 0) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }

    }
}
